package com.steven.ourcontest.forum.api

import com.steven.ourcontest.chat.bean.ReportRes
import com.steven.ourcontest.forum.bean.DiscussInfoList
import com.steven.ourcontest.forum.bean.ForumInfo
import com.steven.ourcontest.forum.bean.ForumInfoList
import com.steven.ourcontest.information.bean.SchoolList
import com.steven.ourcontest.login.helper.LoginController
import com.steven.ourcontest.net.ApiTransformer
import com.steven.ourcontest.net.BaseResponse
import com.steven.ourcontest.net.EmptyResponse
import com.steven.ourcontest.net.NetConstants
import io.reactivex.Observable
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.Field
import java.util.concurrent.TimeUnit

class ForumApi private constructor() {
    private val mRetrofit: Retrofit
    private val mApiService: ForumApiService

    companion object {
        const val DEFAULT_TIMEOUT = 5
        val instance: ForumApi by lazy {
            ForumApi()
        }
    }

    init {
        val builder = OkHttpClient.Builder()
        builder.connectTimeout(
            DEFAULT_TIMEOUT.toLong(),
            TimeUnit.SECONDS
        )
        builder.addInterceptor {
            val request = it.request()
            val requestBuilder = request.newBuilder()
            requestBuilder.header("uid", LoginController.userId.toString())
                .header("userType", LoginController.userType)
            return@addInterceptor it.proceed(requestBuilder.build())
        }
        mRetrofit = Retrofit.Builder()
            .client(builder.build())
            .baseUrl(NetConstants.FORUM_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
        mApiService = mRetrofit.create(ForumApiService::class.java)
    }

    fun queryHotForumList(page: Long): Observable<BaseResponse<ForumInfoList>> {
        return mApiService.queryHotForumList(page)
    }

    fun queryNewForumList(page: Long): Observable<BaseResponse<ForumInfoList>> {
        return mApiService.queryNewForumList(page)
    }

    fun addForum(body: RequestBody): Observable<BaseResponse<EmptyResponse>> {
        return mApiService.addForum(body)
    }

    fun queryForumByFid(fid: Long): Observable<BaseResponse<ForumInfo>> {
        return mApiService.queryForumByFid(fid)
    }

    fun addHistory(fid:Long): Observable<BaseResponse<EmptyResponse>> {
        return mApiService.addHistory(fid)
    }

    fun queryDiscussList(forumId: Long): Observable<BaseResponse<DiscussInfoList>> {
        return mApiService.queryDiscussList(forumId)
    }

    fun addDiscuss(forumId: Long,content:String): Observable<BaseResponse<EmptyResponse>> {
        return mApiService.addDiscuss(forumId,content)
    }

    fun addComplain(body: RequestBody): Observable<EmptyResponse> {
        return mApiService.addComplain(body).compose(ApiTransformer())
    }

    fun queryComplain(lastCreateTime: Long): Observable<ReportRes> {
        return mApiService.queryComplain(lastCreateTime).compose(ApiTransformer())
    }

    fun updateComplain(id: Long): Observable<EmptyResponse> {
        return mApiService.updateComplain(id).compose(ApiTransformer())
    }
}